package com.controller;

import com.dto.JsonResult;
import com.entity.UserInfo;
import com.service.UserService;
import com.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Yadasu
 * @date 2020/12/21 15:44
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 获取所有的用户信息
     * @return
     */
    @GetMapping("/api/admin/user")
    public JsonResult listUsers() {

        return JsonResult.buildSuccess(200,userService.list());
    }

    /**
     *更新用户状态（可用/不可用）
     * @return
     */
    @PutMapping("/api/admin/user/status")
    public JsonResult updateUserStatus(@RequestBody UserInfo requestUser) {
        userService.updateUserStatus(requestUser);
        return JsonResult.buildSuccess(200,"用户状态更新成功");
    }
    /**
     *重置密码
     * @return
     */
    @PutMapping("/api/admin/user/password")
    public JsonResult resetPassword(@RequestBody UserInfo requestUser) {
        userService.resetPassword(requestUser);
        return JsonResult.buildSuccess(200,"重置密码成功");
    }
    /**
     *编辑用户信息
     * @return
     */
    @PutMapping("/api/admin/user")
    public JsonResult editUser(@RequestBody UserVo userVo) {
        userService.editUser(userVo);
        return JsonResult.buildSuccess(200,"修改用户信息成功");
    }


    /**
     *新增用户
     * @return
     */
    @PostMapping("/api/admin/user/add")
    public JsonResult addUser(@RequestBody UserVo userVo) {
        String username = userVo.getUserInfo().getUsername();
        boolean flag = userService.isExist(username);
        if (flag) {
            return JsonResult.buildFailure(400,"用户已存在");
        } else {
            userService.addUser(userVo);
            return JsonResult.buildSuccess(200,"新增用户成功");
        }
    }
}
